package testcasetool.transformation.presentation;

import java.util.List;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import proyecto.testcasetool.Scenario;

import testcasetool.transformation.uml.utp.Action_TestComponent;
import testcasetool.transformation.uml.utp.Parameter_DataPool;
import testcasetool.transformation.uml.utp.TestCase_TestCase;
import testcasetool.transformation.uml.utp.TestScenario_TestContext;

public class TestCaseHtmlViewer {

	public static StringBuffer generateHtmlTestCase(TestCase_TestCase tc) {
		StringBuffer buffer = new StringBuffer();
		
		buffer.append(getHtmlInitialization());
		buffer.append(getTestCaseTitleRow(tc.getName()));
		buffer.append(getTestCaseDescriptionRow(tc.getTestScenario().getTestTrail()));
		buffer.append(getTestCasePreConditionRow(tc.getPreconditions()));
		buffer.append(getTestCaseStepsSection(tc.getTestScenario()));
		buffer.append(getTestCaseExpectedResultRow(tc.getTestScenario().getPostCondition()));
		buffer.append(getHtmlTableEnd());
		
		return buffer;

	}

	private static String getHtmlTableEnd() {
		return "	</tbody>\n"+
				"</table>\n";
	}

	private static String getTestCaseExpectedResultRow(String postCondition) {
		return "		<tr>\n"+
				"			<td><strong>Expected Results</strong></td>\n"+
				"			<td>" + getHtmlFriendlyValue(postCondition) + "</td>\n"+
				"		</tr>\n";
	}

	private static String getTestCaseStepsSection(TestScenario_TestContext testScenario) {
		StringBuffer sb = new StringBuffer();
		sb.append("		<tr>\n");
		sb.append("			<td colspan=\"2\"><strong>Steps</strong></td>\n");
		sb.append("		</tr>\n");
		sb.append("		<tr>\n");
		sb.append("			<td colspan=\"2\">\n");
		sb.append("			<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" style=\"width:100%\">\n");
		sb.append("				<tbody>\n");
		sb.append("					<tr>\n");
		sb.append("						<td colspan=\"1\" rowspan=\"3\" style=\"text-align: center;\"><strong>ID</strong></td>\n");
		sb.append("						<td colspan=\"1\" rowspan=\"3\" style=\"text-align: center;\"><strong>Name</strong></td>\n");
		sb.append("						<td colspan=\"1\" rowspan=\"3\" style=\"text-align: center;\"><strong>Description</strong></td>\n");
		sb.append("						<td colspan=\"6\" rowspan=\"1\" style=\"text-align: center;\"><strong>Operational Variables</strong></td>\n");
		sb.append("						<td colspan=\"1\" rowspan=\"3\" style=\"text-align: center;\"><strong>Expected Result</strong></td>\n");
		sb.append("					</tr>\n");
		sb.append("					<tr>\n");
		sb.append("						<td rowspan=\"2\" style=\"text-align: center;\"><strong>Type</strong></td>\n");
		sb.append("						<td rowspan=\"2\" style=\"text-align: center;\"><strong>Data Type</strong></td>\n");
		sb.append("						<td rowspan=\"2\" style=\"text-align: center;\"><strong>Name</strong></td>\n");
		sb.append("						<td rowspan=\"2\" style=\"text-align: center;\"><strong>Condition</strong></td>\n");
		sb.append("						<td colspan=\"2\" style=\"text-align: center;\"><strong>Test values</strong></td>\n");
		sb.append("					</tr>\n");
		sb.append("					<tr>\n");
		sb.append("						<td style=\"text-align: center;\"><strong>Generated</strong></td>\n");
		sb.append("						<td style=\"text-align: center;\"><strong>Singular</strong></td>\n");
		sb.append("					</tr>\n");
		List<Action_TestComponent> actions = testScenario.getTestSteps();
		for (Action_TestComponent a : actions) {
			sb.append(generateStepRow(a));
			
		}
		sb.append("				</tbody>\n");
		sb.append("			</table>\n");
		sb.append("			</td>\n");
		sb.append("		</tr>\n");
		
		return sb.toString();
	}

	private static StringBuffer generateStepRow(Action_TestComponent a) {
		StringBuffer sb = new StringBuffer();
		sb.append("					<tr>\n");
		sb.append("						<td>" + a.getActionId() + "</td>\n");
		sb.append("						<td>" + getHtmlFriendlyValue(a.getActionName()) + "</td>\n");
		sb.append("						<td>" + getHtmlFriendlyValue(a.getActionDescription()) + "</td>\n");
		
		sb.append(generateParameterColumnsData(a.getActionParameter()));
		sb.append("						<td>" + getHtmlFriendlyValue(a.getActionExpectedResult()) + "</td>\n");
		sb.append("					</tr>\n");
		return sb;
	}

	private static StringBuffer generateParameterColumnsData(Parameter_DataPool pdp) {
		StringBuffer sb = new StringBuffer();
		if (pdp != null) {
			sb.append("						<td>" + getHtmlFriendlyValue(pdp.getType()) + "</td>\n");
			sb.append("						<td>" + getHtmlFriendlyValue(pdp.getDataType()) + "</td>\n");
			sb.append("						<td>"  + 
						" <span class=\"ui-icon ui-icon-info\" style=\"float: left;\" title=\""+ 
						getHtmlFriendlyValue(pdp.getDescription()) +"\" ></span>" +
						getHtmlFriendlyValue(pdp.getName()) + "</td>\n");
			sb.append("						<td>" + getHtmlFriendlyValue(pdp.getCondition()) + "</td>\n");
			sb.append("						<td>" + getHtmlFriendlyValue(pdp.getGeneratedTestValues()) + "</td>\n");
			sb.append("						<td>" + getHtmlFriendlyValue(pdp.getSingularTestValues()) + "</td>\n");
		} else {
			sb.append("						<td>&nbsp;</td>\n");
			sb.append("						<td>&nbsp;</td>\n");
			sb.append("						<td>&nbsp;</td>\n");
			sb.append("						<td>&nbsp;</td>\n");
			sb.append("						<td>&nbsp;</td>\n");
			sb.append("						<td>&nbsp;</td>\n");
		}
		return sb;
	}

	private static String getHtmlFriendlyValue(String value) {
		if (value != null && value.length()>0) {
			return StringEscapeUtils.escapeHtml4(value);
		} else {
			return "&nbsp;";
		}
	}

	private static String getTestCasePreConditionRow(String preconditions) {
		return "		<tr>\n" +
				"			<td><strong>Preconditions</strong></td>\n" +
				"			<td>" + getHtmlFriendlyValue(preconditions) + "</td>\n" +
				"		</tr>\n";
	}

	private static String getTestCaseDescriptionRow(List<Scenario> trail) {
		return "		<tr>\n"+
				"			<td><strong>Test Trail</strong></td>\n"+
				"			<td>" + StringEscapeUtils.escapeHtml4(StringUtils.join(trail, " -> ")) + "</td>\n"+
				"		</tr>\n";
	}

	private static String getHtmlInitialization() {
		return "<table border=\"1\" align=\"center\" cellpadding=\"1\" cellspacing=\"0\" style=\"width:100%\">\n"
			 + "	<tbody>\n";
	}

	private static String getTestCaseTitleRow(String name) {
		return "		<tr>\n"+
				"			<td><strong>Name</strong></td>\n"+
				"			<td>"+ getHtmlFriendlyValue(name) + "</td>\n"+
				"		</tr>\n";
	}
}
